﻿using Maxi.Areas.Sistema.Models;
using Maxi.Models;
using Maxi.Models.Banco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Maxi.Areas.Sistema.Controllers
{
    public class TurmaController : Controller
    {
        // GET: Sistema/Turma
        private Conexao con = new Conexao();
        // GET: Sistema/Aluno
        public ActionResult Index()
        {
            List<string> breadcrumbs = new List<string>();
            Dictionary<string, string> dic = new Dictionary<string, string>();

            dic.Add("@Url.Content('~/Alunos')", "Alunos");

            breadcrumbs.Add("Home");
            breadcrumbs.Add("Turma");

            ViewBag.Breadcrumbs = breadcrumbs;
            return View();
        }

        public ActionResult Novo()
        {
            var model = new TurmaModel();
            ViewBag.Grupos = con.Tb_Grupo.ToList();
            return View("NovoEditar", model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Salvar(TurmaModel model)
        {
            Tb_Turma turma = null;

            if (con.Tb_Turma.Any(x => x.CodTurma == model.codigo))
            {
                turma = con.Tb_Turma.Where(x => x.CodTurma == model.codigo).SingleOrDefault();
                con.Entry(turma).State = System.Data.Entity.EntityState.Modified;
            }
            else
            {
                turma = new Tb_Turma();
                string codigo = model.codigo;
                turma.CodTurma = codigo;
                con.Tb_Turma.Add(turma);
            }

            turma.Nivel = model.nivel;
            turma.Serie = model.serie.ToInt32();
            turma.AnoLetivo = model.anoletivo.ToInt32();
            turma.Descricao = model.descricao;
            turma.Turno = model.turno;
            con.SaveChanges();

            return RedirectToAction("Index");
        }

        public ActionResult Editar(string id)
        {
            Tb_Aluno model = con.Tb_Aluno.Where(x => x.UsuarioId == id).FirstOrDefault();
            ViewBag.Grupos = con.Tb_Grupo.ToList();
            return View("NovoEditar", model);
        }

        public ActionResult Excluir(string id)
        {
            Tb_Usuario aluno = con.Tb_Usuario.Where(x => x.Id == id).FirstOrDefault();
            con.Tb_Usuario.Remove(aluno);
            con.SaveChanges();

            return RedirectToAction("Index");
        }

        [HttpGet]
        public JsonResult List(TableRequestModel req)
        {
            Conexao con = new Conexao();

            IQueryable<Tb_Turma> list = con.Tb_Turma.OrderBy(x => x.CodTurma);

            if (String.IsNullOrWhiteSpace(req.sSearch) == false)
            {
                list = list.Where(x =>
                        x.CodTurma.Contains(req.sSearch) ||
                        x.AnoLetivo.Equals(req.sSearch) ||
                        x.Descricao.Contains(req.sSearch) ||
                        x.Nivel.Equals(req.sSearch) ||
                        x.Serie.Equals(req.sSearch) 
                    );
            }

            int total = list.Count();

            list = list.Skip(req.iDisplayStart).Take(req.iDisplayLength);

            var model = new TableResponseModel
            {
                sEcho = req.sEcho,
                iTotalRecords = list.Count(),
                iTotalDisplayRecords = total,
                aaData = from r in list.ToList()
                         select new
                         {
                             Cod = r.CodTurma,
                             Descricao = r.Descricao,
                             Nivel = r.Nivel,
                             Serie = r.Serie,
                             Turno = r.Turno,
                             AnoLetivo = r.AnoLetivo,
                             EditUrl = Url.RouteUrl("sistema.turma.edit", new { id = r.CodTurma }),
                             DeleteUrl = Url.RouteUrl("sistema.turma.excluir", new { id = r.CodTurma })
                         }
            };


            return Json(model, JsonRequestBehavior.AllowGet);

        }
    }
}